﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;
using System.Data;

namespace QunYing.DAL
{
    public partial class DeviceInfo
    {
        /// <summary>
        /// 查询用户所拥有水族箱设备
        /// </summary>
        /// <param name="strUserName"></param>
        /// <returns></returns>
        public DataTable QueryDevice(string strUserName)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ICSA编号,名称 from V_UserDevice where 用户ID=@用户ID");
            SqlParameter[] parameters = {
					new SqlParameter("@用户ID",strUserName)
             };
            return DbHelperSQL.Query(strSql.ToString(), parameters).Tables[0];
        }

        /// <summary>
        /// 查询水族箱设备信息
        /// </summary>
        /// <param name="strUserName"></param>
        /// <returns></returns>
        public DataTable QueryDeviceInfo(string strIcsa)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 * from V_UserDevice where ICSA编号=@ICSA编号");
            SqlParameter[] parameters = {
					new SqlParameter("@ICSA编号",strIcsa)
             };
            return DbHelperSQL.Query(strSql.ToString(), parameters).Tables[0];
        }

        /// <summary>
        /// 更新设备信息
        /// </summary>
        public bool UpdateDevice(QunYing.Model.DeviceInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update DeviceInfo set ");
            strSql.Append("鱼类品种=@鱼类品种,");
            strSql.Append("水草品种=@水草品种,");
            strSql.Append("水族箱地址=@水族箱地址,");
            strSql.Append("品牌=@品牌,");
            strSql.Append("型号=@型号,");
            strSql.Append("箱体尺寸=@箱体尺寸,");
            strSql.Append("名称=@名称");
            strSql.Append(" where ICSA编号=@ICSA编号");
            SqlParameter[] parameters = {
					new SqlParameter("@鱼类品种", SqlDbType.NVarChar,200),
					new SqlParameter("@水草品种", SqlDbType.NVarChar,200),
					new SqlParameter("@水族箱地址", SqlDbType.NVarChar,200),
					new SqlParameter("@ICSA编号", SqlDbType.NVarChar,20),
                    new SqlParameter("@品牌", SqlDbType.NVarChar,200),
                    new SqlParameter("@型号", SqlDbType.NVarChar,200),
                    new SqlParameter("@箱体尺寸", SqlDbType.NVarChar,200),
                    new SqlParameter("@名称", SqlDbType.NVarChar,200)
                 };
            parameters[0].Value = model.鱼类品种;
            parameters[1].Value = model.水草品种;
            parameters[2].Value = model.水族箱地址;
            parameters[3].Value = model.ICSA编号;
            parameters[4].Value = model.品牌;
            parameters[5].Value = model.型号;
            parameters[6].Value = model.箱体尺寸;
            parameters[7].Value = model.名称;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
